package com.hertz.appr.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;





public class BaseDao extends HibernateDaoSupport {
	Logger log = Logger.getLogger(BaseDao.class);

	/**
	 * @see com.ku.dao.Dao#saveObject(java.lang.Object)
	 */
	public void saveObject(Object o) throws Exception{
		try {
			getHibernateTemplate().saveOrUpdate(o);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("baseDao error: exception=" + e + ";saveObject(o)" + o.toString());
			throw e;
		}

	}

	/**
	 * @see com.ku.dao.Dao#getObject(java.lang.Class, java.io.Serializable)
	 */
	public Object getObject(Class clazz, Serializable id) {
		Object o = getHibernateTemplate().get(clazz, id);
		return o;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ku.dao.Dao#getObjects(java.lang.Class)
	 */
	public List getObjects(Class clazz) {
		return getHibernateTemplate().loadAll(clazz);
	}

	public List find(String hql, Object... values){
        return getHibernateTemplate().find(hql, values);
    }

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ku.dao.Dao#query(java.lang.String, int, int)
	 */
	public List query(final String hql, final int start, final int pageSize) {
		if (log.isDebugEnabled())
			log.debug("findAdAdminsing AdAdmin list");
		List list = null;
		try {
			list = getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query q = session.createQuery(hql);
					q.setFirstResult(start);
					q.setMaxResults(pageSize);
					List list = q.list();
					return list;
				}
			});
			log.debug("find successful");
		} catch (Exception re) {
			log.error("find failed exception=" + re + ";hql=" + hql);
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ku.dao.Dao#queryObjects(java.lang.String)
	 */
	public List queryObjects(String hql) {
		try {
			return getHibernateTemplate().find(hql);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.error("baseDao error:exception=" + e + ";hql = " + hql);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ku.dao.Dao#saveOrUpdate(java.lang.Object)
	 */
	public void saveOrUpdate(Object object) throws Exception{
		try {
			this.getHibernateTemplate().saveOrUpdate(object);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("baseDao error:exception=" + e + ";object=" + object.toString());
			throw e;
		}

	}

	public void update(Object object) throws Exception{
		try {
			this.getHibernateTemplate().update(object);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("baseDao error:exception=" + e + ";object=" + object.toString());
			throw e;
		}


	}

	public List findByExample(Object instance) {
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (Exception re) {
			log.error("find by example failed,exception=" + re + ";instance=" + instance.toString());
			return null;
		}
	}
}
